package cat.music.ut.ui.fragment

import android.annotation.SuppressLint
import android.content.Intent
import android.media.MediaScannerConnection
import android.os.Bundle
import android.util.Log
import android.view.View
import android.widget.TextView
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import cat.music.ut.R
import cat.music.ut.adapter.MultiDownloadAdapter
import cat.music.ut.base.BaseFragment
import cat.music.ut.comm.ext.setEmptyView
import cat.music.ut.component.log
import cat.music.ut.databinding.ActivityDownloadNewBinding
import cat.music.ut.databinding.FragmentIndexBinding
import cat.music.ut.ui.activity.MusicActivity
import com.arialyy.aria.core.Aria
import com.arialyy.aria.core.download.DownloadEntity
import com.arialyy.aria.core.download.DownloadTaskListener
import com.arialyy.aria.core.task.DownloadTask
import org.greenrobot.eventbus.EventBus

class DownloadFragment : BaseFragment<ActivityDownloadNewBinding>(), DownloadTaskListener {
    private val mActivity by lazy { requireActivity() as MusicActivity }
    override fun getViewBinding(): ActivityDownloadNewBinding {
        return ActivityDownloadNewBinding.inflate(layoutInflater)
    }


    override fun observe() {

    }

    override fun initData() {
    }

    override fun initClick() {

    }
    private var mData = ArrayList<DownloadEntity>()
    private lateinit var mAdapter: MultiDownloadAdapter
    override fun initView() {
//        Aria.download(this).register()
        val temps = Aria.download(this).allNotCompleteTask
        if (temps != null && temps.isNotEmpty()) {
            mData.addAll(temps as ArrayList<DownloadEntity>)
        }
        mAdapter = MultiDownloadAdapter(requireActivity(), mData)
        mAdapter.init()
        vb.rvDownloadList.layoutManager = LinearLayoutManager(requireActivity())
        vb.rvDownloadList?.adapter = mAdapter
        vb.rvDownloadList.setEmptyView()
//        findViewById<View>(R.id.download_record).setOnClickListener {
//            startActivity(Intent(this,SearchActivity::class.java))
//        }
//        findViewById<View>(R.id.download_record1).setOnClickListener {
//            startActivity(Intent(this,DlJiluActivity::class.java))
//        }
        initDownloadData()
    }

    private fun initDownloadData() {
        if (mData.size == 0) {
            vb.tvDownload.visibility = View.VISIBLE
            vb.rvDownloadList.visibility = View.GONE
        } else {
            vb.tvDownload.visibility = View.GONE
            vb.rvDownloadList.visibility = View.VISIBLE
        }
    }

    override fun onWait(task: DownloadTask?) {
        if (task ==null)return
        mAdapter.updateState(task!!.entity)
        Log.d(TAG, task.taskName + ", " + task.state)
    }
    val TAG = "下载界面"
    override fun onPre(task: DownloadTask?) {
        if (task ==null)return
        mAdapter.updateState(task!!.entity)
        Log.d(TAG, task.taskName + ", " + task.state)
    }

    override fun onTaskPre(task: DownloadTask?) {
        if (task ==null)return
        mAdapter.updateState(task!!.entity)
        Log.d(TAG, task.taskName + ", " + task.state)
    }

    override fun onTaskResume(task: DownloadTask?) {
        if (task ==null)return
        Log.d(TAG, task!!.taskName + ", " + task.state)
        mAdapter.updateState(task.entity)
    }

    override fun onTaskStart(task: DownloadTask?) {
        if (task ==null)return
        Log.d(TAG, task!!.taskName + ", " + task.state)
        mAdapter.updateState(task.entity)
    }

    override fun onTaskStop(task: DownloadTask?) {
        if (task ==null)return
        mAdapter.updateState(task!!.entity)
    }

    override fun onTaskCancel(task: DownloadTask?) {
        if (task ==null)return
        mAdapter.updateState(task!!.entity)
    }

    override fun onTaskFail(task: DownloadTask?, e: Exception?) {
        if (task ==null)return
        mAdapter.updateState(task!!.entity)
    }

    override fun onTaskComplete(task: DownloadTask?) {
        if (task ==null)return
        EventBus.getDefault().post(task.entity)
        mAdapter.updateState(task!!.entity)
        try {
            MediaScannerConnection.scanFile(
                requireActivity(),
                arrayOf<String>(task.filePath),
                null
            ) { path, uri ->
                // 扫描完成后的回调处理
            }
        }catch (e:Exception){
            e.printStackTrace()
        }
    }

    override fun onTaskRunning(task: DownloadTask) {
        if (task ==null)return
//        log(task!!.entity.toString())
        mAdapter.setProgress(task.entity)
    }

    override fun onNoSupportBreakPoint(task: DownloadTask?) {

    }

    override fun onResume() {
        super.onResume()
        Aria.download(this).register()
        val temps = Aria.download(this).allNotCompleteTask
        if (temps != null && temps.isNotEmpty()) {
            mData.clear()
            mData.addAll(temps as ArrayList<DownloadEntity>)
            mAdapter.notifyDataSetChanged()
            Log.e(TAG, "onResume: "+mData.size )
            mAdapter.init()
        }

        initDownloadData()
    }

    override fun onPause() {
        "onPause".log()
        super.onPause()
        Aria.download(this).unRegister()
    }
}